﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.DepartmentRelegate;

public partial class BasicBase_DepartmentRelegate_RelegateAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        BindDepartmentClass();
        BindDepartment();
        BindParent();
        BindCity();
        BindRegion();
        if (Request["ID"] != null)
        {
            ddlDepartmentClass.SelectedValue = Request["id"].Substring(0, 4) + "000";
            BindDepartment();
            ddlDepartment.SelectedValue = Request["id"];
        }
        BindData();
    }

    private void BindDepartmentClass()
    {
        ddlDepartmentClass.DataSource = Department.GetList(" departmentId like '%000' and departmentId<>'1300000'");
        ddlDepartmentClass.DataBind();
    }
    private void BindDepartment()
    {
        string ClassId = ddlDepartmentClass.SelectedValue;
        ddlDepartment.DataSource = Department.GetList(" departmentId = '" + ClassId + "' or departmentId like '" + ClassId.Substring(0, 4) + "3__' or departmentId like '" + ClassId.Substring(0, 4) + "2__' or(departmentId like '" + ClassId.Substring(0, 4) + "1__' and ShortName like '%特勤%' ) ");
        ddlDepartment.DataBind();
    }

    void BindParent()
    {
        string ClassId = ddlDepartmentClass.SelectedValue;
        List<DepartmentInfo> list = Department.GetList(" departmentId ='1300000' or departmentId='" + ClassId.Substring(0, 4) + "000' or departmentId like '" + ClassId.Substring(0, 4) + "2__'");
        ddlParent.Items.Clear();
        ddlParent.Items.Add(new ListItem("---单位信息----------", ""));
        ddlParent.DataSource = list;
        ddlParent.DataBind();
    }

    void BindCity()
    {
        ddlCity.Items.Clear();
        ddlCity.Items.Add(new ListItem("--地区--", ""));
        List<CodeItemInfo> list = CodeItem.GetList(" parentId='SS030000' order by itemID");
        ddlCity.DataSource = list;
        ddlCity.DataBind();
    }

    void BindRegion()
    {
        ddlRegion.Items.Clear();
        if (ddlCity.SelectedValue != "")
        {
            List<CodeItemInfo> list = CodeItem.GetList(" parentId='" + ddlCity.SelectedValue + "' order by itemID");
            ddlRegion.DataSource = list;
            ddlRegion.DataBind();
        }
        else
        {
            ddlRegion.Items.Add(new ListItem("---域区---", ""));
        }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        RelegateDataContext db = new RelegateDataContext();
        DepartmentRelegate relegate = db.DepartmentRelegate.Where(o => o.DepartmentId == ddlDepartment.SelectedValue).FirstOrDefault();

        if (relegate != null)
        {
            try
            {
                // DepartmentRelegate relegate = db.DepartmentRelegate.Where(o => o.DepartmentId == Request["id"]).First();
                relegate.DepartmentId = ddlDepartment.SelectedValue.Trim();
                relegate.DepartmentLevel = 0;

                relegate.IsCitylevel = Convert.ToInt32(cbLevel.Checked);
                relegate.parentId = ddlParent.SelectedValue;
                relegate.RegionId = ddlRegion.SelectedValue;
                relegate.SetDate = DateTime.Now;
                relegate.SetUserName = User.Identity.Name;

                db.SubmitChanges();
                Prompt.Text = "提示：保存成功！";
            }
            catch
            {
                Prompt.Text = "提示：保存失败！";
            }
        }
        else
        {
            try
            {
                relegate = new DepartmentRelegate();
                relegate.DepartmentId = ddlDepartment.SelectedValue.Trim();
                relegate.DepartmentLevel = 0;
                relegate.Id = Guid.NewGuid();
                relegate.IsCitylevel = Convert.ToInt32(cbLevel.Checked);
                relegate.parentId = ddlParent.SelectedValue;
                relegate.RegionId = ddlRegion.SelectedValue;
                relegate.SetDate = DateTime.Now;
                relegate.SetUserName = User.Identity.Name;
                db.DepartmentRelegate.InsertOnSubmit(relegate);
                db.SubmitChanges();
                Prompt.Text = "提示：保存成功！";
            }
            catch
            {
                Prompt.Text = "提示：保存失败！";
            }
        }
    }
    protected void ddlDepartmentClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDepartment();
        BindParent();
        BindData();
    }
    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindRegion();

    }
    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {

        RelegateDataContext db = new RelegateDataContext();
        DepartmentRelegate relegate = db.DepartmentRelegate.Where(o => o.DepartmentId == ddlDepartment.SelectedValue).FirstOrDefault();
        if (relegate != null)
        {
            BindParent();
            ddlParent.SelectedValue = relegate.parentId;
            CodeItemInfo Region = CodeItem.GetCodeItem(relegate.RegionId);

            ddlCity.SelectedValue = Region.ParentID;


            BindRegion();
            ddlRegion.SelectedValue = relegate.RegionId;
            cbLevel.Checked = Convert.ToBoolean(relegate.IsCitylevel);
        }
        else
        {
            ddlParent.SelectedValue = "";
            ddlCity.SelectedValue = "";
            BindRegion();

            cbLevel.Checked = false;
        }
    }
}
